package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.EncryptUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户登录
 *
 * @create 2020/11/16 0016 19:21
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    //获得当前用户登录的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() {
        //当spring-security完成认证后,会将当前用户信息保存到框架提供的上下文对象
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //新增用户
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('USER_ADD')")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        EncryptUtil encryptUtil = EncryptUtil.getInstance();
        user.setPassword(encryptUtil.BCrypt(user.getPassword()));
        try {
            userService.add(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    //分页
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return userService.pageQuery(queryPageBean);
    }

    //编辑
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        EncryptUtil encryptUtil = EncryptUtil.getInstance();
        user.setPassword(encryptUtil.BCrypt(user.getPassword()));
        try {
            userService.edit(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }


    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findById(Integer id) {
        try {
            User user = userService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    @RequestMapping("/findRoleIdsByUserId")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findRoleIdsByUserId(Integer id) {
        try {
            List<Integer> list = userService.findRoleIdsByUserId(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    //删除
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }
    @RequestMapping("/findMenu")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findMenu(String userName) {
       try {
           //根据用户名查询
           Set<Map<String,Object>> set=userService.findMenu(userName);
           return new Result(true,MessageConstant.GET_MENU_SUCCESS,set);
       }catch (Exception e){
           e.printStackTrace();
           return new Result(false,MessageConstant.GET_MENU_FAIL);
       }
    }
}
